package cn.northsea.mapper;

import cn.northsea.pojo.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;


@Repository
public interface ProductMapper {

    List<MaterialVo> findMaterialAll(@Param("likename") String likename, @Param("start") String start, @Param("end") String end);


    List<ProductVo> findProductAll(@Param("likename") String likename, @Param("start") String start, @Param("end") String end);


    List<ProductPlanVo> findProductPlanAll(@Param("likename") String likename, @Param("start") String start, @Param("end") String end);

    @Select("select mp.*,m.material_name,m.unit\n" +
            "from erp_material_product mp left join erp_material m\n" +
            "on mp.material_id = m.material_id \n" +
            "where mp.product_id = #{productId} and " +
            "mp.product_plan_id = #{productPlanId} and mp.deleted = 0")
    List<MaterialProductVo> toFindMaterial(
            @Param("productId") Integer productId,
            @Param("productPlanId") Integer productPlanId);


    List<GetMaterialVo> getMaterial(@Param("likename") String likename, @Param("start") String start, @Param("end") String end);

    @Select("select * from erp_material_category")
    List<MaterialCategory> findMaterialCategoryAll();

    @Select("select * from erp_material where material_name = #{value}")
    Material check(String materialName);

    @Insert("insert into erp_material values (null,#{materialName},#{unit},#{materialCategoryId},#{createAt},#{materialPrice},0)")
    void addMaterial(Material material);

    @Select("select * from erp_material_category where material_category_name = #{value}")
    Material checkMaterialCategoryName(String materialCategoryName);

    @Insert("insert into erp_material_category values (null,#{materialCategoryName},#{createAt})")
    void addMaterialCategory(MaterialCategory materialCategory);

    void delMaterial(Integer[] ids);

    @Select("select * from erp_material where material_id = #{value}")
    Material findMaterialByMid(Integer materialId);

    @Update("update erp_material \n" +
            "set material_name = #{materialName},material_category_id=#{materialCategoryId},unit=#{unit},material_price= #{materialPrice}\n" +
            "where material_id=#{materialId}")
    void updateMaterial(Material material);

    @Select("select * from erp_product_category")
    List<ProductCategory> findProductCategoryAll();

    @Select("select * from erp_product where product_name = #{value}")
    Product checkProductName(String productName);

    @Select("select * from erp_product_category where product_category_name = #{value}")
    Product checkProductCategoryName(String productCategoryName);

    @Insert("insert into erp_product values (null,#{productName},#{productSize},#{createAt},#{productCategoryId},0,#{productMoney})")
    void addProduct(Product product);

    @Insert("insert into erp_product_category values (null,#{productCategoryName},#{createAt})")
    void addProductCategory(ProductCategory productCategory);


    void delProduct(Integer[] ids);

    @Select("select * from erp_product where product_id = #{value}")
    Product findProductByPid(Integer productId);


    @Update("update erp_product \n" +
            "set product_name = #{productName},product_size=#{productSize},create_at = #{createAt},product_category_id = #{productCategoryId},deleted=0,product_money = #{productMoney}\n" +
            "where product_id=#{productId}")
    void updateProduct(Product product);

    @Select("select * from erp_product")
    List<Product> findProductList();

    @Insert("insert into erp_product_plan values (null,#{productId},#{quantity},0,#{createAt},0)")
    void addProductPlan(ProductPlan productPlan);

    @Update("update erp_product_plan set deleted = 1 where product_plan_id=#{value}")
    void delProductPlan(Integer productPlanId);

    @Select("select * from erp_product_plan where product_plan_id = #{value}")
    ProductPlan findProductPlanById(Integer productPlanId);

    @Update("update erp_product_plan \n" +
            "set product_id = #{productId},quantity=#{quantity},status = 0,create_at = #{createAt},deleted=0\n" +
            "where product_plan_id=#{productPlanId}")
    void updateProductPlan(ProductPlan productPlan);

    @Select("select * from erp_material where deleted = 0")
    List<Material> findMaterialList();

    @Insert("insert into erp_material_product values (null,#{productId},#{materialId},#{materialQuantity},#{createAt},#{status},#{deleted},#{productPlanId})")
    void addProductMaterial(MaterialProduct materialProduct);

    void delProductMaterial(Integer[] ids);

    @Select("select * from erp_material_product where material_product_id = #{value}")
    MaterialProduct findProductMaterialById(Integer materialProductId);

    @Update("update erp_material_product \n" +
            "set product_id = #{productId},material_id=#{materialId},material_quantity = #{materialQuantity},create_at=#{createAt}\n" +
            "where material_product_id=#{materialProductId}")
    void toUpdateProductMaterial(MaterialProduct materialProduct);

    @Update("update erp_get_material set status = 1 where get_material_id = #{value}")
    void certainMaterial(Integer getMaterialId);

    @Select("select * from erp_get_material where material_id = #{value} and status = 1")
    List<GetMaterial> findGetMaterialByMid(Integer materialId);

    @Update("update erp_get_material set material_quantity = material_quantity - #{material_quantity} where get_material_id = #{getMaterialId}")
    int updateGetMaterial(@Param("material_quantity") Double materialQuantity, @Param("getMaterialId") Integer materialId);

    @Update("update erp_material_product set status = 1 where material_product_id = #{materialProductId}")
    void updateMaterialProduct(MaterialProduct materialProduct);

    @Select("select gm.material_quantity from erp_get_material gm where gm.material_id = #{value}")
    double findGetMaterialQuantity(Integer materialId);

    @Select("select * from erp_get_material where material_id = #{value} and status = 1;")
    GetMaterial findGetMaterialByMId(Integer materialId);

    @Select("select * from erp_material_product where product_id = #{productId} and product_plan_id = #{productPlanId} and deleted = 0")
    List<MaterialProduct> findMaterialProductByPid(@Param("productId") Integer productId, @Param("productPlanId") Integer productPlanId);

    @Select("select * from erp_material_product where product_id = #{productId} and product_plan_id = #{productPlanId} and deleted = 0 and status = 1")
    List<MaterialProduct> findMaterialProductByPid2(@Param("productId") Integer productId, @Param("productPlanId") Integer productPlanId);

    @Update("update erp_product_plan set status = 1 where product_plan_id = #{value}")
    void toProduct(Integer productPlanId);

    @Update("update erp_product_plan set status = 3 where product_plan_id = #{value}")
    void ProductPlanOver(Integer productPlanId);

    @Update("update erp_product_plan set pid = #{pid} where product_plan_id = #{productPlanId}")
    void getPid(@Param("productPlanId") Integer productPlanId, @Param("pid") long pid);

    @Select("select * from erp_warehouse")
    List<Warehouse> findWarehouseAll();

    @Insert("insert into erp_warehouseIn values (null,null,#{productId},#{warehouseId},#{quantity},#{warehouseInTime})")
    void InWarehouse(WarehouseIn warehouseIn);

    @Select("select * from erp_warehouse_product")
    List<WarehouseProduct> findWarehouseProductAll();

    @Insert("insert into erp_warehouse_product values (null,#{productId},#{warehouseProductQuantity}" +
            ",#{warehouseId},500,5000)")
    void inWarehouseProduct(@Param("productId") Integer productId,
                            @Param("warehouseProductQuantity") Integer quantity,
                            @Param("warehouseId") Integer warehouseId);

    @Insert("insert into erp_get_material values (null,#{materialId},#{materialQuantity}" +
            ",#{createAt},#{status})")
    void toAddGetMaterial(GetMaterial getMaterial);

}
